package serverdata.ch.home.www.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;



public class HttpData {


	private final String comando;
	private String url;
	private List<String> parametri;


	public HttpData(BufferedReader in) throws IOException {

		String riga = null;

		riga = in.readLine();

		System.out.println("riga: "+riga);
		if(riga != null){
			StringTokenizer st = new StringTokenizer(riga);
			comando = st.nextToken().toUpperCase();
			url = st.nextToken().toUpperCase();
			st = new StringTokenizer(url, "?&");
			url = st.nextToken();
			parametri = new ArrayList<String>();
			while(st.hasMoreTokens()){
				parametri.add(st.nextToken());
			}
			System.out.println("URL: "+getUrl()+" Comando: "+getComando()+" parametri: "+getParametri());

			while(in.ready()){
				System.out.println(in.readLine());
			}
		}
		else{
			comando = "nessuno";
			url = "nessuno";
			parametri = new ArrayList<String>();
			parametri.add("nessuno");
		}


	}


	public String getComando() {
		return comando;
	}


	public String getUrl() {
		return url;
	}

	public HashMap<String, String> getParametri(){
		HashMap<String, String> mappa = new HashMap<String, String>();
		for(String param : parametri){
			StringTokenizer st = new StringTokenizer(param, "=");
			mappa.put(st.nextToken(), st.nextToken());
		}

		return mappa;
	}


	public static String createHeaderHTTP(String dati, int tipo, String type){

		String type1 = type;
		
		if(type == null ){
			type1 = "html";
		}
		StringBuilder header = new StringBuilder();

		header.append("HTTP/1.0 "+ tipo + " "+CodeHTTP.getMsg(tipo)+"\r\n");
		header.append("Date : " + new Date().toGMTString() + "\r\n");
		header.append("Content-Type : " + "text/"+type1+"\r\n");
		header.append("Content-Length : "+ dati.length()+"\r\n");
		header.append("\r\n");


		return header.toString();

	}





}
